package com.fyw.xmcamera;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lib.funsdk.support.FunSupport;
import com.lib.funsdk.support.OnFunRegisterListener;

public class MainActivity extends BaseActivity implements OnFunRegisterListener {

    private ListView mainListView;

    @Override
    protected void handleToolbar(ToolbarHelper toolbarHelper) {
        toolbarHelper.setTitle("设备管理");
        toolbarHelper.setFinish().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initParam(Bundle param) {
        FunSupport.getInstance().init(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(View view) {
        mainListView = view.findViewById(R.id.mainListView);
        mainListView.setAdapter(new MainAdapter(this));

        // 注册监听(用户注册相关)
        FunSupport.getInstance().registerOnFunRegisterListener(this);
        FunSupport.getInstance().registerByPhone("lc502", "0000000000", "", "");
    }

    @Override
    public void doBusiness(Context mContext) {
        mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        startActivity(DeviceListActivity.class);
                        break;
                    case 1:
                        startActivity(DeviceListLanActivity.class);
                        break;
                    case 2:
                        startActivity(WifiConfigActivity.class);
                        break;
                }
            }
        });
    }

    @Override
    public void onRequestSendCodeSuccess() {
    }

    @Override
    public void onRequestSendCodeFailed(Integer errCode) {
    }

    @Override
    public void onRegisterNewUserSuccess() {
    }

    @Override
    public void onRegisterNewUserFailed(Integer errCode) {
        //showToast(FunError.getErrorStr(errCode));
    }

    @Override
    public void onUserNameFine() {
    }

    @Override
    public void onUserNameUnfine(Integer errCode) {
    }

    //----------------------------------------------------------------------------------------------
    public class MainAdapter extends BaseAdapter {
        private Context context;
        private int[] img = {R.mipmap.icon_main_setting, R.mipmap.icon_main_list_1,
                R.mipmap.icon_main_list_2};
        private String[] name = {"设备列表（序列号）", "设备列表（局域网）", "快速配置WIFI"};

        public MainAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return img.length;
        }

        @Override
        public Object getItem(int i) {
            return img[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.item_main_lv, null);
                viewHolder = new ViewHolder();
                viewHolder.cameraImg = view.findViewById(R.id.itemMainImg);
                viewHolder.cameraName = view.findViewById(R.id.itemMainName);
                viewHolder.itemMainView = view.findViewById(R.id.itemMainView);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.cameraName.setText(name[i]);
            viewHolder.cameraImg.setImageResource(img[i]);

            if (name.length == i + 1)
                viewHolder.itemMainView.setVisibility(View.INVISIBLE);
            else
                viewHolder.itemMainView.setVisibility(View.VISIBLE);

            return view;
        }

        class ViewHolder {
            TextView cameraName;
            ImageView cameraImg;
            View itemMainView;
        }
    }
}
